package com.shujia.io;

import com.shujia.liaotian.bean.User;

import java.io.*;
import java.util.Scanner;

public class Demo2ObjectStream {
    public static void main(String[] args) throws Exception {
        /**
         * 对象流   读写对象   java所有对象    对象必须可以序列化    implements Serializable
         *
         */

        FileOutputStream fileOutputStream = new FileOutputStream("java/data/a.txt");

        User user = new User();
        user.setName("张三");
        user.setPassword("123456");

        ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);

        objectOutputStream.writeUTF("对象流");

        //将对象保存到文件
        objectOutputStream.writeObject(user);

        objectOutputStream.writeInt(100);

        objectOutputStream.flush();

        objectOutputStream.close();
        fileOutputStream.close();


        /**
         *
         * 对象输出流输出的数据需要通过对象输入流进行读取
         *
         * 读取数据的时候必须按照写数据的顺序读取
         *
         */


        FileInputStream fileInputStream = new FileInputStream("java/data/a.txt");
        ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);

        System.out.println(objectInputStream.readUTF());

        User o = (User) objectInputStream.readObject();

        System.out.println(o);

        System.out.println(objectInputStream.readInt());

        objectInputStream.close();


        /**
         *
         * 打印流
         *
         */


        //控制台输出流
        PrintStream out = System.out;
        out.println("");


        PrintStream printStream = new PrintStream(new FileOutputStream("java/data/print.txt"));


        printStream.println("java");
        printStream.println("spark");
        printStream.println("hive");

        //控制台字节输入流
        InputStream in = System.in;
        Scanner scanner = new Scanner(System.in);

    }
}
